package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.yang.entity.Attendance;
import com.sanxiau.dormitory.yang.entity.Dorm;
import com.sanxiau.dormitory.yang.service.AttendanceService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;
    @ResponseBody
    @RequestMapping("/facedelete")
    public ResultInfo facedelete(String base64Data){
        ResultInfo resultInfo = attendanceService.faceshow(base64Data);
        /*System.out.println("resultInfo"+resultInfo);*/
        return resultInfo;
    }
    @RequestMapping("/addAttendance")
    public String addAttendance(String base64Data,String publicIP, Model model, HttpSession session){
        System.out.println("publicIP--------------:"+publicIP);
        int flag = publicIP.indexOf("183.230.207");
        if (flag >= 0) {
            ResultInfo resultInfo = attendanceService.addAttendance(base64Data, model, session);
            model.addAttribute("message", resultInfo.getMessage());
        }else {
            model.addAttribute("message", "请登录校园网进行考勤");
        }
        return "yang/Attendance";
    }
    @RequestMapping("/attendanceRecord")
    public String attendanceRecord(Model model,HttpSession session){
        List<Attendance> attendances = attendanceService.allAttendance();
        model.addAttribute("attendances",attendances);
        return "yang/attendanceRecord";
    }
    @RequestMapping("/hou")
    public String  hou(Model model){
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        //System.out.println("hous:"+hous);
        return "yang/notAttendance";
    }

    @ResponseBody
    @RequestMapping("/dor")
    public List<String>   dor(String hou){
        List<String>  dors = attendanceService.queryAllDors(hou);
        //System.out.println("dors:"+dors);
        return dors;
    }

    @RequestMapping("/cause")
    public String   setCause(String startArea1,String startArea2,String text,HttpSession session,Model model){
        ResultInfo resultInfo = attendanceService.setCause(startArea1,startArea2,text,session);
        //System.out.println("startArea1:"+startArea1+"startArea2:"+startArea2+"text:"+text+"session:"+session.getAttribute("userName"));
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        if ("缺勤原因添加成功".equals(resultInfo.getMessage())){
            model.addAttribute("message","缺勤原因提交成功");
            return "yang/notAttendance";
        }else {
            model.addAttribute("message",resultInfo.getMessage());
            return "yang/notAttendance";
        }

    }

    @RequestMapping("faceshow")
    public String faceshow(){
        return "yang/Attendance";
    }
    @RequestMapping("notAttendance")
    public String notAttendance(){
        return "yang/notAttendance";
    }
    @RequestMapping("test")
    public String test(){
        return "yang/test";
    }



}
